package weather;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;


public class GUI {
	public JFrame mainFrame = new JFrame();
	JComboBox graphType = new JComboBox();
	public LinkedList<String> features = new LinkedList<String>();
	WeatherGrapher wg = new WeatherGrapher();
	JPanel subGraph = new JPanel();
	static Map<String,List<Double>> His_Lows = new HashMap<String,List<Double>>();
	static JTextPane Highs_Lows = new JTextPane();
	static JTextPane Real_Time_Data = new JTextPane();
	static Map<String,Double> Real_Time = new HashMap<String,Double>();
	//Refresh Button
	JButton updateGraph = new JButton();
	GUI() throws Exception {
		
		graphType.addItem("Temperature");
		graphType.addItem("Wind Speed");
		graphType.addActionListener(new graphChoose());
		JTabbedPane pane = new JTabbedPane();
		
		List<Double> defaultVals= new LinkedList<Double>();
		defaultVals.add(null); defaultVals.add(null);
		His_Lows.put("Temperature", new LinkedList<Double>(defaultVals));
		His_Lows.put("Humidity", new LinkedList<Double>(defaultVals));
		His_Lows.put("Pressure", new LinkedList<Double>(defaultVals));
		His_Lows.put("Wind Speed", new LinkedList<Double>(defaultVals));
		Highs_Lows.setText(generateHighsLows());
		JButton getData = new JButton();
		updateGraph.setText("Refresh");
		updateGraph.addActionListener(new graphChoose());		
		getData.addActionListener(new getDataPush());
		getData.setText("Start Data Accumulation");
		subGraph.add(wg.tempGraph(),0);
		subGraph.add(graphType,1);
		subGraph.add(updateGraph,2);
		pane.add("Real Time Data", Real_Time_Data);
		pane.add("Highs and Lows", Highs_Lows);
		pane.add("Graph",subGraph);
		pane.add("Get Data",getData);
		mainFrame.add(pane);
		mainFrame.setTitle("Weather Monitoring Station");
		mainFrame.pack();
		mainFrame.setLocation(250, 250);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setVisible(true);
	}
	
	private static String generateHighsLows() {
		String hiLo = "";
		Set<String> keys = His_Lows.keySet();
		Iterator<String> iterator = keys.iterator();
		while(iterator.hasNext()) {
			String next = iterator.next();
			hiLo = hiLo + next + " HIGH: " + His_Lows.get(next).get(1) + "\n";
			hiLo = hiLo + next + " LOW: " + His_Lows.get(next).get(0) + "\n\n";
		}
		return hiLo;
	}
	
	public static void updateHiLow (String type, double value) {
		if(His_Lows.containsKey(type)) {
			List<Double> hl = His_Lows.get(type);
			if(hl.get(0) == null || hl.get(0) > value) {
				hl.set(0, value);
			}
			if(hl.get(1) == null || hl.get(1) < value) {
				hl.set(1, value);
			}
			Highs_Lows.setText(generateHighsLows());
		}
	}
	
	public static void updateRealTime(String type, double value) {
		Real_Time.put(type, value);
	}
	
	public static void putRealTime() {
		String realTime = "";
		Set<String> keys = Real_Time.keySet();
		Iterator<String> iterator = keys.iterator();
		while(iterator.hasNext()) {
			String type = iterator.next();
			realTime = realTime + type + ": " + Real_Time.get(type) + "\n";
		}
		Real_Time_Data.setText(realTime);
	}
	
	public class graphChoose implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String choice = (String) graphType.getSelectedItem();
			if(choice.equalsIgnoreCase("Temperature")) {
				try {
					subGraph.removeAll();
					subGraph.updateUI();
					subGraph.add(wg.tempGraph(),0);
					subGraph.add(graphType,1);
					updateGraph.setText("Refresh");
					updateGraph.addActionListener(new graphChoose());
					subGraph.add(updateGraph,2);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if(choice.equalsIgnoreCase("Wind Speed")) {
				try {
					subGraph.removeAll();
					subGraph.updateUI();
					subGraph.add(wg.windGraph(),0);
					subGraph.add(graphType,1);
					updateGraph.setText("Refresh");
					updateGraph.addActionListener(new graphChoose());
					subGraph.add(updateGraph,2);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public class getDataPush implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				SerialTest.main(null);
			} catch (Exception e) {
				System.out.println("Could Not Find USB Input, Make sure USB data is not being used by another program.");
			}
		}
		
	}
	
	public static void main(String[] args) throws Exception {
		//GUI gui = new GUI();
		DatabaseHelper db = new DatabaseHelper();
		db.dbClear();
	}
}
